(function () {
    "use strict"; //JavaScript代码标志为“严格模式
    var projectPackageController=function ($scope, $http,$location,$modal,projectService,productService,toolsService,suiteService,downloadService) {
        $scope.vm = {
            id : toolsService.parameter("id", $location.absUrl())
        };
        $scope.loading = false;

        $scope.showAll = function (index) {
            $scope.selectList[index].showList = [];
            var i = 1;
            $scope.selectList[index].isShowAll = true;
            angular.forEach($scope.selectList[index].versionList,function (item) {
                var show = {
                    version : item
                };
                $scope.selectList[index].showList.push(show);
            });

        };

        $scope.getProject = function () {
            projectService.projectGet($scope.vm).success(function (data) {
                if(data.errorMsg != null){
                    $.Huimodalalert(data.errorMsg,1000);
                } else {
                    $scope.project = data.project;
                    if($scope.project.watchedBies != null && $scope.project.watchedBies.length > 0 ){
                        angular.forEach($scope.project.watchedBies ,function (item) {
                            if(item.role == 'PM'){
                                $scope.project.pm = {
                                    picName : item.picName,
                                    picPhone : item.picPhone
                                }
                            } else if(item.role == 'DM'){
                                $scope.project.dm = {
                                    picName : item.picName,
                                    picPhone : item.picPhone
                                }
                            } else {
                                $scope.project.test = {
                                    picName : item.picName,
                                    picPhone : item.picPhone
                                }
                            }
                        });
                    }

                    if($scope.project.suitesContaineds != null && $scope.project.suitesContaineds.length > 0){
                        suiteService.suiteGet({id:$scope.project.suitesContaineds[0].id}).success(function (data) {
                            if(data.errorMsg != null){
                                $.Huimodalalert(data.errorMsg,1000);
                            } else {
                                $scope.suite = data.suite;
                                var strings = data.suite.suiteName.split("-");
                                $scope.suite.suiteName = strings[0];
                                $scope.suite.suiteVersion = strings[1];
                            }
                        });
                    }

                    if($scope.project.productsContaineds != null && $scope.project.productsContaineds.length > 0){
                        $scope.ids = [];
                        angular.forEach($scope.project.productsContaineds,function (item) {
                            $scope.ids.push(item.id);
                        });
                        productService.productFindByIds({ids:$scope.ids}).success(function(data){
                            if(data.errorMsg != null){
                                $.Huimodalalert(data.errorMsg,1000);
                            } else {
                                $scope.selectList = data.result;
                                angular.forEach($scope.selectList,function (item) {
                                    if(item.versionList != null && item.versionList.length > 4){
                                        item.showList = [];
                                        angular.forEach(item.versionList.slice(0,3),function (version) {
                                            var showBean = {
                                                version:version
                                            };
                                            item.showList.push(showBean);
                                        });
                                        item.isShowAll = false;
                                    } else {
                                        item.showList = [];
                                        angular.forEach(item.versionList,function (version) {
                                            var showBean = {
                                                version:version
                                            };
                                            item.showList.push(showBean);
                                        });
                                        item.isShowAll = true;
                                    }
                                });
                            }
                        });
                    }
                }
            });
        };

        $scope.getProject();


        //编辑项目
        $scope.doEdit = function(){
            window.parent.location.href = "projectpage.html?type=edit&id="+$scope.project.id;
        };

        $scope.download = function () {
            $scope.loading = true;
            var param = {
                downloadDirs : []
            };
            var projectId = $scope.project.id;
            var selectList = $scope.selectList;
            var suite=$scope.suite;

            //数据提交
            var record = {
                projectId : projectId,
                downloadedProducts : ""
            };
            angular.forEach(selectList,function (item) {
                angular.forEach(item.showList,function (show) {
                    if(item.selectVersion == null && show.state == true){
                        item.selectVersion = show.version;
                    }
                });
                if(item.selectVersion != null){
                    var product = {
                        id:item.id,
                        type:"2",
                        version:item.selectVersion,
                        path:$scope.path
                    };
                    param.downloadDirs.push(product);
                    record.downloadedProducts += ""+item.productName +":"+item.selectVersion+";";
                }
            });
            if(suite != null && suite.state){
                param.downloadDirs.push({
                    id:suite.id,
                    type:"1",
                    version:suite.suiteVersion,
                    path:$scope.path
                });
                record.downloadedProducts += ""+suite.suiteName +":"+suite.suiteVersion;
            } else {
                var length = record.downloadedProducts.lastIndexOf(";");
                record.downloadedProducts = record.downloadedProducts.substr(0,length);
            }
            if(param.downloadDirs.length == 0){
                $scope.loading = false;
                $.Huimodalalert("未选择产品或套件版本，请检查！",1000);
                return;
            }
            downloadService.fileDownloadDirBatch(param.downloadDirs).success(function (data) {
                if(data.errorMsg != null){
                    $scope.loading = false;
                    $.Huimodalalert(data.errorMsg,1000);
                } else {
                    var fileName = data.fileName;
                    projectService.projectDownloadSave(record).success(function () {
                        $scope.loading = false;
                        if(data.errorMsg != null){
                            $.Huimodalalert(data.errorMsg,1000);
                        } else {
                            //特殊字符+，&转义处理
                            fileName = fileName.replace(/\+/g, "%2B");
                            fileName = fileName.replace(/\&/g, "%26");
                            window.location.href = "file/download?type=1&fileName=" + fileName;
                        }
                    });
                }
            });
        };
    };
    var projectUseController=function ($scope, $http,$location,$modal,projectService) {
        $scope.vm = {};

        //新建项目
        $scope.doCreate = function(){
            window.parent.location.href="projectpage.html?type=create";
        };
        //删除项目
        $scope.doDelete = function(projectItem){

            window.parent.location.href = "projectpage.html?type=delete&id="+projectItem.id;
        };
        $scope.findDetail = function () {
            projectService.projectFindDetail().success(function (data) {
                if(data.errorMsg != null){
                    $.Huimodalalert(data.errorMsg,1000);
                } else {
                    $scope.projectList = data.result;
                    var index = 0;
                    angular.forEach($scope.projectList,function (data) {
                        if(data.watchedBies != null && data.watchedBies.length > 0 ){
                            angular.forEach(data.watchedBies ,function (item) {
                                if(item.role == 'PM'){
                                    $scope.projectList[index].pm = {
                                        name : item.picName,
                                        phone : item.picPhone
                                    }
                                } else if(item.role == 'DM'){
                                    $scope.projectList[index].dm = {
                                        name : item.picName,
                                        phone : item.picPhone
                                    }
                                }
                            });
                        }
                        index++;
                    });
                }
            });
        };

        $scope.findDetail();
    };
    var projectHistoryController=function ($scope, $http,$location,$modal,projectService,toolsService) {
        $scope.vm = {
            projectId : toolsService.parameter("id", $location.absUrl()),
            pageSize:10,
            pageNumber:0,
            totalCount:0
        };
        $scope.downloadRecordList = [];

        projectService.projectGet({id:$scope.vm.projectId}).success(function (data) {
            if(data.errorMsg != null){
                $.Huimodalalert(data.errorMsg,1000);
            } else {
                $scope.project = data.project;
                if($scope.project.watchedBies != null && $scope.project.watchedBies.length > 0 ){
                    angular.forEach($scope.project.watchedBies ,function (item) {
                        if(item.role == 'PM'){
                            $scope.project.pm = {
                                picName : item.picName,
                                picPhone : item.picPhone
                            }
                        } else if(item.role == 'DM'){
                            $scope.project.dm = {
                                picName : item.picName,
                                picPhone : item.picPhone
                            }
                        } else {
                            $scope.project.test = {
                                picName : item.picName,
                                picPhone : item.picPhone
                            }
                        }
                    });
                }
            }
        });

        projectService.projectFindDownloadRecord($scope.vm).success(function (data) {
            if(data.errorMsg != null){
                $.Huimodalalert(data.errorMsg,1000);
            } else {
                var downloadRecordList = data.result.content;
                var index = 0;
                angular.forEach(downloadRecordList,function (item) {
                    if(item.downloadedTime != null){
                        var date = new Date(item.downloadedTime);
                        item.downloadedTime = date.format("yyyy-MM-dd hh:mm:ss");
                    }
                    var productInfoList = item.downloadedProducts.split(";");
                    downloadRecordList[index].recordList = [];
                    angular.forEach(productInfoList,function (productInfo) {
                        var list = productInfo.split(":");
                        var info = {
                            productName : list[0],
                            productVersion : list[1]
                        };
                        downloadRecordList[index].recordList.push(info);
                    });
                    index++;
                });
                $scope.downloadRecordList = downloadRecordList;
            }
        });
    };
    var projectPageController=function ($scope, $http,$location,$modal,projectService,toolsService) {
        $scope.projectList = [];
        $scope.vm = {
            pageSize:10,
            pageNumber:1
        };
        //新建项目
        $scope.doCreate = function() {
            //弹出窗口
            var modalInstance = $modal.open({
                templateUrl: "projectCreate.html",
                controller:projectCreate,
                size:"lg",
                resolve: {
                    items: function () {
                        return {
                            type:'add',
                            title:'新建项目'
                        };
                    }
                }
            });
            modalInstance.result.then(function () {
                window.parent.location.href="projectpage.html";
            });
        };

        //编辑项目
        $scope.doEdit = function(){
            //弹出窗口
            var modalInstance = $modal.open({
                templateUrl: "projectCreate.html",
                controller:projectCreate,
                size:"lg",
                resolve: {
                    items: function () {
                        return {
                            type:'edit',
                            title:'编辑项目',
                            project :$scope.project
                        };
                    }
                }
            });
            modalInstance.result.then(function (data) {
                window.myiframe.location="project_package.html?id="+$scope.project.id;
                $scope.getDetail();
            });
        };

        //删除项目
        $scope.doDelete = function(){
            //弹出窗口
            var modalInstance = $modal.open({
                templateUrl: "projectDelete.html",
                controller:projectDelete,
                size:"lg",
                resolve: {
                    items: function () {
                        return {
                            type:'delete',
                            title:'删除项目',
                            project:$scope.project
                        };
                    }
                }
            });
            modalInstance.result.then(function (data) {
                window.parent.location.href="projectpage.html";
                $scope.getDetail();
            });
        };
        var type = toolsService.parameter("type",$location.absUrl());
        if(type != null){
            $scope.project = {};

            if(type == "create"){
                $scope.doCreate();
            } else{
                var id = toolsService.parameter("id",$location.absUrl());
                if(id != null){
                    projectService.projectGet({id:id}).success(function (data) {
                        if(data.errorMsg != null){
                            $.Huimodalalert(data.errorMsg,1000);
                        } else {
                            $scope.project = data.project;
                            if($scope.project.watchedBies != null && $scope.project.watchedBies.length > 0 ){
                                angular.forEach($scope.project.watchedBies ,function (item) {
                                    if(item.role == 'PM'){
                                        $scope.project.pm = {
                                            picName : item.picName,
                                            picPhone : item.picPhone
                                        }
                                    } else if(item.role == 'DM'){
                                        $scope.project.dm = {
                                            picName : item.picName,
                                            picPhone : item.picPhone
                                        }
                                    } else {
                                        $scope.project.test = {
                                            picName : item.picName,
                                            picPhone : item.picPhone
                                        }
                                    }
                                });
                            }
                            if(type == "edit"){
                                window.myiframe.location="project_package.html?id="+$scope.project.id;
                                $scope.doEdit();
                            } else {
                                $scope.doDelete();
                            }
                        }
                    });
                }
            }
        }

        $scope.getDetail = function(){
            projectService.projectFindDetail($scope.vm).success(function (data) {
                if(data.errorMsg != null){
                    $.Huimodalalert(data.errorMsg,1000);
                } else {
                    $scope.projectList = data.result;
                }
            });
        };
        $scope.getDetail();
        $scope.changeUrl = function (item,index) {
            if(item != null && item != undefined && item.id != null){
                window.myiframe.location.href="project_package.html?id="+item.id;
            } else if(index == 2) {
                window.myiframe.location.href="project_manage.html";
            } else {
                window.myiframe.location.href="project_use.html";
            }
            $(".pointer").removeClass("left-hover").eq(index).addClass("left-hover");
        };
    };
    var projectCreate = ["$scope","$modal", "$modalInstance","items","ProjectService", "ProductService","SuiteService","PersonInChargeService",function ($scope,$modal,$modalInstance,items,projectService,productService,suiteService,personInChargeService) {
        //前台页面数据
        $scope.sc = {
            title : items.title
        };
        if(items.project == null){
            $scope.project = {};
        } else {
            $scope.project = items.project;
            $scope.projectName = items.project.projectName;
        }

        personInChargeService.picFindByRole({roleList : ['PM']}).success(function (data) {
           if(data.errorMsg != null){
               $.Huimodalalert(data.errorMsg,1000);
           } else {
               $scope.pmList = data.result;
               if($scope.project.pm != null){
                   var pm = $scope.project.pm;
                   angular.forEach($scope.pmList,function (item) {
                       if(item.picName == pm.picName && item.picPhone == pm.picPhone){
                           $scope.project.pm = item;
                       }
                   });
               }
           }
        });

        personInChargeService.picFindByRole({roleList : ['DM']}).success(function (data) {
            if(data.errorMsg != null){
                $.Huimodalalert(data.errorMsg,1000);
            } else {
                $scope.dmList = data.result;
                if($scope.project.dm != null){
                    var dm = $scope.project.dm;
                    angular.forEach($scope.dmList,function (item) {
                        if(item.picName == dm.picName && item.picPhone == dm.picPhone){
                            $scope.project.dm = item;
                        }
                    })
                }
            }
        });

        personInChargeService.picFindByRole({roleList : ['FORMAL_DEV','FORMAL_TEST','ASSISTANT_DEV','ASSISTANT_TEST']}).success(function (data) {
            if(data.errorMsg != null){
                $.Huimodalalert(data.errorMsg,1000);
            } else {
                $scope.testList = data.result;
                if($scope.project.test != null){
                    var test = $scope.project.test;
                    angular.forEach($scope.testList,function (item) {
                        if(item.picName == test.picName && item.picPhone == test.picPhone){
                            $scope.project.test = item;
                        }
                    })
                }
            }
        });

        productService.productFindDetail().success(function (data) {
            if(data.errorMsg != null){
                $.Huimodalalert(data.errorMsg,1000);
            } else {
                $scope.productList = data.result;
                angular.forEach($scope.productList,function (product) {
                    product.state = false;
                    angular.forEach($scope.project.productsContaineds,function (item) {
                       if(item.id == product.id){
                           product.state = true;
                       }
                    });
                });
            }
        });
        suiteService.suiteFindByBch({suiteName:"bch"}).success(function (data) {
            if(data.errorMsg != null){
                $.Huimodalalert(data.errorMsg,1000);
            } else {
                $scope.suiteList = data.result;
                var index = 0;
                angular.forEach($scope.suiteList,function (item) {
                   var suite = [];
                   suite = item.suiteName.split("-");
                   $scope.suiteList[index].suiteVersion = suite[1];
                   if($scope.project.suitesContaineds != null && $scope.project.suitesContaineds.length > 0 &&
                       item.id == $scope.project.suitesContaineds[0].id){
                       $scope.project.suiteIndex = index;
                   }
                   index++;
                });
            }
        });
        //数据提交
        $scope.doSave = function () {

            if($scope.projectName == null || $scope.projectName == undefined
                || $.trim($scope.projectName) == ""){
                $.Huimodalalert("请输入项目名称！",1000);
                return;
            } else {
                $scope.project.projectName = $scope.projectName;
            }
            $scope.project.suitesContaineds = [];
            $scope.project.productsContaineds = [];
            $scope.project.watchedBies = [];
            if($scope.project.suiteIndex != null){
                var index = $scope.project.suiteIndex;
                var item = $scope.suiteList[index];
                $scope.project.suitesContaineds = [{id:item.id,suiteName:item.suiteName}];
            }
            if($scope.productList.length > 0){
                angular.forEach($scope.productList,function (item) {
                    if(item.state == true){
                        var product = {
                            id:item.id,
                            productName:item.productName,
                            productVersion:item.productVersion
                        };
                        $scope.project.productsContaineds.push(product);
                    }
                });
            }

            if($scope.project.projectSetupDate != null && $scope.project.projectSetupDate instanceof Date){
                $scope.project.projectSetupDate = $scope.project.projectSetupDate.format("yyyy-MM-dd");
            }

            if($scope.project.pm != null && $scope.project.pm.id != null){
                $scope.project.watchedBies.push($scope.project.pm);
            }

            if($scope.project.dm != null && $scope.project.dm.id != null){
                $scope.project.watchedBies.push($scope.project.dm);
            }

            if($scope.project.test != null && $scope.project.test.id != null){
                $scope.project.watchedBies.push($scope.project.test);
            }

            projectService.projectCreate($scope.project).success(function (data) {
                if(data.errorMsg != null){
                    $.Huimodalalert(data.errorMsg,1500);
                } else {
                    $.Huimodalalert("项目保存成功！",1000);
                    $modalInstance.close($scope.sc);
                }
            });
        };

        $scope.doClose = function () {
            $scope.sc = {};
            $modalInstance.dismiss('cancel');
        };
    }];

    var projectDelete = ["$scope","$modal", "$modalInstance","items","ProjectService",function ($scope,$modal,$modalInstance,items,projectService) {
        //前台页面数据
        $scope.sc = {
            title : items.title
        };
        $scope.project = items.project;
        //数据提交
        $scope.doDelete = function () {
            projectService.projectDelete({id:$scope.project.id}).success(function (data) {
                if(data.errorMsg != null){
                    $.Huimodalalert(data.errorMsg,1500);
                } else {
                    $.Huimodalalert("项目删除成功！",1000);
                    $modalInstance.close($scope.sc);
                }
            });
        };

        $scope.doCancel = function () {
            $scope.sc = {};
            window.parent.location.href="projectpage.html";
        };
    }];

    angular.module("cmss.project",[])
        .controller("ProjectPackageController",["$scope","$http","$location","$modal","ProjectService","ProductService","toolsService","SuiteService","DownloadService",projectPackageController])
        .controller("ProjectUseController",["$scope","$http","$location","$modal","ProjectService",projectUseController])
        .controller("ProjectHistoryController",["$scope","$http","$location","$modal","ProjectService","toolsService",projectHistoryController])
        .controller("ProjectPageController",["$scope","$http","$location","$modal","ProjectService","toolsService",projectPageController])
})();
